package com.aglhz.nature.presenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.BrandIntrouctionEvent;
import com.aglhz.nature.modle.BrandImage;
import com.aglhz.nature.modle.item.BrandData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.BrandView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class BrandIntroductionPresenter extends BasePresenter{
	private static String TAG = BrandIntroductionPresenter.class.getName();
	
	private BrandView brandView;
	private BrandImage brandImage;
	private BrandData brandData;

	public BrandIntroductionPresenter(BrandView view) {
		super();
		brandView = view;
	}

	public void loadImageView() {
		// load remote data
		String brandId=brandView.setBrandId();
		Log.e("AAAA--------------------------", brandId);
		
		this.loadBrandImage(brandId);
		
	}
	
	
	
	
	public void  showBrandImage(){
		String url=brandImage.getData().getBrandURL();
		Log.e("DDDDDD--------------------------", url);
		brandView.showWebViewImage(brandImage.getData().getBrandURL());
		Log.e("DDDDDDTTTTTTTTT--------------------------", brandImage.getData().getBrandURL());
	}
	//要改成get id
	public String showMoreUrl(){
		String BrandId=brandImage.getData().getBrandId();
		return BrandId;
		
	}

		public void setTitle(){
			Log.e("-brandImage.getData().getTitle()--",brandImage.getData().getTitle());
			brandView.setTitle(brandImage.getData().getTitle());
		}

	

	// /********************view operation*******************///
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventBrandIntrouctionThread(
			BrandIntrouctionEvent event) {

		String msg = event.getMessage();
		Log.i(TAG, "BrandIntrouctionEvent:" + msg);
		// update UI By event
		if (BrandIntrouctionEvent.EVENT_SHOW_IMAGE == msg) {
			// update grid view
			Log.d(TAG, "brandImage.getData()---yyyyyyyyyyyyy---"
					+ brandImage.getData());

			this.showBrandImage();
			this.showMoreUrl();
			this.setTitle();

			Log.d(TAG, "brandImage.getData()--gggggggggggggggg---"
					+ brandImage.getData());
		}

	}
	
	
	
	
	
	
	// /*********************private method*******************///
		private void loadBrandImage(String BrandId) {

			
			
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_BRAND+BrandId;
			Log.e("Brandintro--------------urlid------------", url);

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					try {
						Log.e(TAG, "response" + responseString);

						Gson gson = new Gson();

						BrandImage bImage = gson
								.fromJson(responseString,
										BrandImage.class);
						brandImage = (BrandImage) bImage
								.clone();

						Log.d(TAG, "brandImage---mmm-----"
								+ brandImage);
						Log.d(TAG, "brandImage---111-----"
								+ brandImage.getOther());
						Log.d(TAG, "brandImage---eeeee---"
								+ brandImage.getData());

						brandData = brandImage
								.getData();

						Log.d(TAG, "Data---mcp-----"
								+ brandData);
						// send event
						BrandIntrouctionEvent event = new BrandIntrouctionEvent(
								BrandIntrouctionEvent.EVENT_SHOW_IMAGE);
						EventBus.getDefault().post(event);

						Log.e(TAG, "success----------------------------"
								+ "success");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {

					Log.e(TAG, "failure");
				}

			});

		}

}
